﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Documents.Services;
using SmartSpace.Web;

namespace SmartSpace.Documents.UI.Pages
{
    public partial class UpLoadDocument : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_LoadFile_Click(object sender, EventArgs e)
        {
            Guid guid = Request.GetGuid("document_foler_id");
            UploadDocument(guid);

            SsPage.Return(this, new { id = Guid.NewGuid().ToString() });
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           
        }

        public void UploadDocument(Guid folderId)
        {
            string docPath = this.loadFile.FileName;
            Document doc = new Document();
            doc.Id = Guid.NewGuid();
            doc.Code = "";
            doc.FileName = docPath.Substring(0, docPath.LastIndexOf('.'));
            doc.Extension = docPath.Substring(docPath.LastIndexOf(".") + 1).ToLower();

            doc.Size = loadFile.FileBytes.Length;
            doc.Data = loadFile.FileBytes;
            doc.CreatedDate = DateTime.Now.ToLocalTime();
            doc.CreateUser = this.Page.User.Identity.Name;
            doc.FolderId = folderId;
            doc.Description = doc_description.Value;

            DocServices services = new DocServices();
            services.SaveDocument(doc);

        }
    }
}